package com.mf.controller;

import com.mf.entity.po.Dictionary;
import com.mf.entity.vo.ListKeyValue;
import com.mf.exceptions.GraceException;
import com.mf.result.Result;
import com.mf.service.IDictionaryService;
import com.mf.utils.VerifyUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 数据字典
 *
 * @author mf
 * @since 2024-07-05
 */
@RestController
@RequestMapping("/dictionary")
@Tag(name = "数据字典")
public class DictionaryController {
    @Resource
    private IDictionaryService dictionaryService;

    @Operation(summary = "新增/编辑")
    @PostMapping("add")
    public Result<String> add(@RequestBody @Valid Dictionary dictionary) {
        if (VerifyUtils.isEmpty(dictionary.getId())) {
            dictionaryService.add(dictionary);
        }else{
            dictionaryService.updateById(dictionary);
        }
        return Result.success();
    }

    @Operation(summary = "根据编号查询")
    @GetMapping("findByCode")
    public Result<List<ListKeyValue<Dictionary>>> findBuCode(String codes) {
        return Result.success(dictionaryService.findByCode(codes));
    }

    @Operation(summary = "删除字典")
    @PostMapping("delete")
    public Result<Boolean> delete(@RequestBody List<Integer> ids) {
        if(ids.isEmpty()){
            GraceException.display("请选择要删除的数据");
            return Result.success(false);
        }

        return Result.success(dictionaryService.delete(ids));
    }

    @Operation(summary = "启用/禁用")
    @GetMapping("changeStatus")
    public Result<Integer> changeStatus(@RequestParam("status") Integer status, @RequestParam("id") Integer id) {
        return Result.success(dictionaryService.changeStatus(id, status));
    }
}
